# encoding:utf-8
import sublime, sublime_plugin

class PhpCmsInsertCategoryCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		pc_start = '{pc:content action="category" catid="$catid" order="listorder DESC" num="10"}\n{loop $data $r}\n'
		pc_end   = '\n{/loop}\n{/pc}'
		sels = self.view.sel()
		for sel in sels:
			self.view.insert(edit, sel.begin(), pc_start)
			self.view.insert(edit, sel.end() + len(pc_start), pc_end)

class PhpCmsInsertCategoryAllCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		pc_start = '{pc:content action="category" catid="$catid" order="listorder DESC" start="" num="10" thumb="1" moreinfo="1" where=""}'
		pc_start += '\n{loop $data $r}\n'
		pc_end   = '\n{/loop}\n{/pc}'
		sels = self.view.sel()
		for sel in sels:
			self.view.insert(edit, sel.begin(), pc_start)
			self.view.insert(edit, sel.end() + len(pc_start), pc_end)

class PhpCmsInsertListsCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		pc_start = '{pc:content action="lists" catid="$catid" order="listorder DESC" num="10"}'
		pc_start += '\n{loop $data $r}\n'
		pc_end   = '\n{/loop}\n{/pc}'
		sels = self.view.sel()
		for sel in sels:
			self.view.insert(edit, sel.begin(), pc_start)
			self.view.insert(edit, sel.end() + len(pc_start), pc_end)

class PhpCmsInsertListsAllCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		pc_start = '{pc:content action="lists" catid="$catid" order="listorder DESC" start="" num="10" thumb="1" moreinfo="1" where=""}'
		pc_start += '\n{loop $data $r}\n'
		pc_end   = '\n{/loop}\n{/pc}'
		sels = self.view.sel()
		for sel in sels:
			self.view.insert(edit, sel.begin(), pc_start)
			self.view.insert(edit, sel.end() + len(pc_start), pc_end)

class PhpCmsInsertRelationCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		pc_start = '{pc:content action="relation" relation="$relation" catid="$catid" num="5" keywords="$rs[keywords]"}'
		pc_start += '\n{loop $data $r}\n'
		pc_end   = '\n{/loop}\n{/pc}'
		sels = self.view.sel()
		for sel in sels:
			self.view.insert(edit, sel.begin(), pc_start)
			self.view.insert(edit, sel.end() + len(pc_start), pc_end)

class PhpCmsInsertFriendLinkCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		pc_start = '{pc:link action="type_list" siteid="1" linktype="1" order="listorder DESC" num="8" typeid="4" }'
		pc_start += '\n{loop $data $r}\n'
		pc_end   = '\n{/loop}\n{/pc}'
		sels = self.view.sel()
		for sel in sels:
			self.view.insert(edit, sel.begin(), pc_start)
			self.view.insert(edit, sel.end() + len(pc_start), pc_end)

class PhpCmsInsertBlockCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		pc = '{pc:block pos=""}'
		sels = self.view.sel()
		for sel in sels:
			self.view.insert(edit, sel.begin(), pc)

class PhpCmsInsertGetCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		pc_start = '{pc:get sql="" cache="3600" page="$page" dbsource="" return="data"}'
		pc_start += '\n{loop $data $r}\n'
		pc_end   = '\n{/loop}\n{/pc}'
		sels = self.view.sel()
		for sel in sels:
			self.view.insert(edit, sel.begin(), pc_start)
			self.view.insert(edit, sel.end() + len(pc_start), pc_end)

class HtmlDeleteHyphenCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		_size = 0
		sels = self.view.find_all('-{3,}')
		for sel in sels:
			_new_region = sublime.Region(sel.a - _size, sel.b - _size)	#修正替换第一个后region坐标点错误的问题
			self.view.replace(edit, _new_region, '--')
			_size += sel.size() -2

class PhpCmsInsertPositionCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		pc_start = '{pc:content action="position" posid="2" order="listorder DESC" num="10"}\n{loop $data $r}\n'
		pc_end   = '\n{/loop}\n{/pc}'
		sels = self.view.sel()
		for sel in sels:
			self.view.insert(edit, sel.begin(), pc_start)
			self.view.insert(edit, sel.end() + len(pc_start), pc_end)